class Solution
{
    int aim;
    vector<vector<int>> ret;
    vector<int> path;
public:
    vector<vector<int>> combinationSum(vector<int>& nums, int target)
    {
        aim = target;
        dfs(nums, 0, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int pos, int sum)
    {
        if (sum == aim)
        {
            ret.push_back(path);
            return;
        }

        if (sum > aim || pos == nums.size()) return;
        for (int k = 0; k * nums[pos] + sum <= aim; k++)
        {
            if (k) path.push_back(nums[pos]);
            dfs(nums, pos + 1, sum + k * nums[pos]);
        }
        for (int k = 1; k * nums[pos] + sum <= aim; k++)
        {
            path.pop_back();
        }
    }
};


class Solution
{
    int aim;
    vector<vector<int>> ret;
    vector<int> path;
public:
    vector<vector<int>> combinationSum(vector<int>& nums, int target)
    {
        aim = target;
        dfs(nums, 0, 0);
        return ret;
    }
    void dfs(vector<int>& nums, int pos, int sum)
    {
        if (sum == aim)
        {
            ret.push_back(path);
            return;
        }

        if (sum > aim || pos == nums.size()) return;
        for (int i = pos; i < nums.size(); i++)
        {
            path.push_back(nums[i]);
            dfs(nums, i, sum + nums[i]);
            path.pop_back();
        }
    }
};